package com.fanyouzhao.eshop.portal.controller;

import com.fanyouzhao.eshop.core.pojo.Banner;
import com.fanyouzhao.eshop.core.utils.JsonResult;
import com.fanyouzhao.eshop.portal.exception.BusinessException;
import com.fanyouzhao.eshop.portal.service.BannerService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: fanyouzhao
 * @date: 17:32 2021/6/30
 */
@RestController
@RequestMapping("/banner")
@Api(tags = "轮播广告模块")
public class BannerController {

    @Resource
    BannerService bannerService;

    @PostMapping("/getAll")
    public JsonResult getAll(@RequestParam("position") String postion) throws BusinessException {
        List<Banner> banners = bannerService.findAllByPosition(postion);
        JsonResult jsonResult = new JsonResult();
        if(banners.size()>0){
            jsonResult.setCode("1");
            jsonResult.setData(banners);
        }else{
            throw  new BusinessException("没有符合条件的广告图");
        }
        return jsonResult;
    }

    @PostMapping("/add")
    public JsonResult add(@RequestBody Banner banner){
        bannerService.insertOrUpdate(banner);
        return new JsonResult("1","增加成功");
    }

    @PostMapping("/selectOne")
    public JsonResult selectOne(Banner banner){
        Banner bannerBack = bannerService.selectOne(banner);
        return new JsonResult("1", "查询成功", bannerBack);
    }

}
